<?php
declare (strict_types=1);

namespace app\common\model;


/**
 * @mixin \think\Model
 */
class User extends Base
{
    protected $autoWriteTimestamp = 'int';

    // 定义全局的查询范围
    protected $globalScope = ['status'];
    protected $append = ['user_type_name', 'status_name'];

    /**
     * 管理员
     */
    const USER_TYPE_1 = 1;
    /**
     * 学生
     */
    const USER_TYPE_10 = 10;

    static public function getUserTypeStatus($status, $default = '未知错误')
    {
        $info = [
            self::USER_TYPE_1 => '管理员',
            self::USER_TYPE_10 => '学生',
        ];

        return preg_match('/^all$/i', $status) ? $info : (isset($info[$status]) ? $info[$status] : $default);
    }

    /**
     * scopeStatus #全局-查询范围-状态
     *
     * @param $query
     * @auth kouweihong
     * @date 2020/11/26
     */
    public function scopeStatus($query)
    {
        $query->where('status', '>', self::STATUS_F_1);
    }

    /**
     * getUserTypeNameAttr #
     *
     * @param $value
     * @param $data
     * @auth kouweihong
     * @date 2020/11/24
     * @return mixed|string|string[]
     */
    public function getUserTypeNameAttr($value, $data)
    {
        return self::getUserTypeStatus($data['user_type'] . '');
    }

    /**
     * user10 #
     *
     * @auth kouweihong
     * @date 2020/11/30
     * @return \think\model\relation\HasOne
     */
    public function user10()
    {
        return $this->hasOne(User10Extend::class, 'user_id', 'id')->joinType('left');
    }

    /**
     * encryptPassword #加密-密码
     *
     * @param string $password
     * @auth kouweihong
     * @date 2020/11/30
     * @return string
     */
    public static function encryptPassword($password = '123456')
    {
        return md5(md5($password));
    }
}
